package com.yycx.module.bus.provider.service.hy.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.hy.BusPolicySaleman;
import com.yycx.module.bus.provider.mapper.hy.BusPolicySalemanMapper;
import com.yycx.module.bus.provider.service.hy.BusPolicySalemanService;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.module.system.provider.service.SysFormulaService;
import org.springframework.stereotype.Service;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.query.CriteriaQuery;
import org.springframework.transaction.annotation.Propagation;
import com.yycx.common.mybatis.query.CriteriaSave;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 政策详情接口实现类
 *
 * @author flyme
 * @date 2023-07-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusPolicySalemanServiceImpl extends BaseServiceImpl<BusPolicySalemanMapper, BusPolicySaleman> implements BusPolicySalemanService {

    @Resource
    private SysFormulaService sysFormulaService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusPolicySaleman bps, EntityMap extra) {
        bps.setCompanyId(OpenHelper.getCompanyId());
        return ResultBody.ok();
    }

    @Override
    public ResultBody beforeEdit(CriteriaUpdate<BusPolicySaleman> cu, BusPolicySaleman bps, EntityMap extra) {
        return super.beforeEdit(cu, bps, extra);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusPolicySaleman> cq, BusPolicySaleman bps, EntityMap requestMap) {
        return ResultBody.ok();
    }


    @Override
    public BusPolicySaleman findByPolicyDrugIdAndSaleManId(Long policyDrugId, Long saleManId, Date sendDate) {
        CriteriaQuery<BusPolicySaleman> cq = new CriteriaQuery(BusPolicySaleman.class);
        cq.eq(true, "policyDrugId", policyDrugId);
        cq.eq(true, "saleManId", saleManId);
        cq.and(e -> e.isNull("endDate").or().ge(true, "endDate", sendDate));
        return getOne(cq,false);
    }

    @Override
    public ResultBody afterAdd(CriteriaSave cs, BusPolicySaleman busPolicySaleman, EntityMap extra) {
        String saleManIds = cs.getParams("saleManIds");
        if (FlymeUtils.isNotEmpty(saleManIds)) {
            String[] saleManIdArray = saleManIds.split(",");
            int i = 0;
            for (String saleManId : saleManIdArray) {
                BusPolicySaleman policySaleman = new BusPolicySaleman();
                BeanUtil.copyProperties(busPolicySaleman, policySaleman);
                policySaleman.setSaleManId(Long.parseLong(saleManId));
                policySaleman.setPolicySaleManId(IdWorker.getId());
                if (i > 0) {
                    saveOrUpdate(policySaleman);
                }
                i++;
            }
        }
        return super.afterAdd(cs, busPolicySaleman, extra);
    }
}
